package com.hst.capacity.facade.rest;

import cn.hutool.core.util.StrUtil;
import com.hst.capacity.application.biz.ExpressOrderBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.driver.DriverEntity;
import com.hst.capacity.domain.model.request.dispatch.DispatchDriverParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchParam;
import com.hst.capacity.domain.model.request.express.*;
import com.hst.capacity.domain.model.request.price.ExpressPriceParam;
import com.hst.capacity.domain.model.request.price.ExpressRePriceParam;
import com.hst.capacity.domain.model.request.price.ExpressStpPriceParam;
import com.hst.capacity.domain.model.response.DefaultVO;
import com.hst.capacity.domain.model.response.order.OrderPriceVO;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  快车订单录入模块Controller，包括快车，站点 CRUD，检索等。
 *
 * */
@RequestMapping("/order/express")
@RestController
public class ExpressOrderController {

    @Autowired
    private ExpressOrderBiz expressControlBiz;


    /**
     * 获取用户所有快车订单列
     */
    @PostMapping("/list/user")
    private DefaultVO findList(@RequestBody @Valid ExListUserParam param){
        return success(expressControlBiz.findExOrderListByUserMobile(param));
    }

    /**
     * 查询城市下所有快车订单
     */
    @PostMapping("/list/region")
    private DefaultVO findListByRegion(@RequestBody @Valid ExListRegionParam param){
        return success(expressControlBiz.findExOrderListByRegionCode(param));
    }

    /**
     *获取快车订单价格
     */
    @PostMapping("/price")
    private DefaultVO expressCommonPrice(@RequestBody @Valid ExpressPriceParam exPriceParam){

        OrderPriceVO vo = new OrderPriceVO();
        if (Constants.ExSubBizType.InnerCity == exPriceParam.getSubBizType()) {
            vo = expressControlBiz.getExpressPrice(exPriceParam);
        }
        if (Constants.ExSubBizType.StationToStation == exPriceParam.getSubBizType()) {
            if (StrUtil.isEmpty(exPriceParam.getIdStation()))
                throw new BusinessException(4001,"汽车站点ID[idStation]不能为空！");
            vo = expressControlBiz.getExpressStpPrice(exPriceParam);
        }
        return success(vo);
    }

    /**
     * 创建订单
     * @param param
     * @return
     */
    @PostMapping("/create")
    private DefaultVO createCommonExpressOrder(@RequestBody @Valid ExpressCreateOrderParam param){

        if (Constants.ExSubBizType.StationToStation == param.getSubBizType()) {
            if (StrUtil.isEmpty(param.getIdStation()))
                throw new BusinessException(4001,"汽车站点ID[idStation]不能为空！");
        }

        return success(expressControlBiz.createExpressOrder(param).getIdOrder());
    }

    /**
     * 计算订单价格/里程/耗时
     * @param param
     * @return
     */
    @PostMapping("/stp/price")
    private DefaultVO expressStpPrice(@RequestBody @Valid ExpressPriceParam param){
        return success(expressControlBiz.getExpressStpPrice(param));
    }

    /**
     * 创建订单
     * @param param
     * @return
     */
    @PostMapping("/stp/save")
    private DefaultVO saveExpressStpOrder(@RequestBody @Valid ExpressCreateOrderParam param){
        return success(expressControlBiz.saveExpressStpOrder(param).getIdOrder());
    }

    /**
     * 创建订单
     * @param param
     * @return
     */
    @PostMapping("/stp/create")
    private DefaultVO createExpressStpOrder(@RequestBody @Valid ExpressCreateOrderParam param){
        return success(expressControlBiz.createExpressStpOrder(param).getIdOrder());
    }

    /**
     * 创建订单
     * @param param
     * @return
     */
    @PostMapping("/stp/update")
    private DefaultVO updateExpressStpOrder(@RequestBody @Valid ExpressCreateOrderParam param){
        return success(expressControlBiz.updateExpressStpOrder(param).getIdOrder());
    }

    /**
     * 获取今日录单信息
     * @param param
     * @return
     */
    @PostMapping("/list/new")
    private DefaultVO getNewExOrderList(@RequestBody @Valid ExListNewParam param){
        return success(expressControlBiz.getNewExOrderList(param));
    }

    /**
     * 获取指定城市今日录单信息
     * @param param
     * @return
     */
    @PostMapping("/list/region/today")
    private DefaultVO getNewExOrderListByRegionCode(@RequestBody @Valid ExListNewParam param){
        return success(expressControlBiz.getNewExOrderListByRegionCode(param));
    }

    /**
     * 订单检索查询
     * @param param
     * @return
     */
    @PostMapping("/search")
    private DefaultVO exOrderSearch(@RequestBody @Valid ExListParam param){
        boolean historyFlg = true;
        if (StrUtil.isEmpty(param.getHistoryKbn()))
            historyFlg = false;
        return success(expressControlBiz.exOrderSearch(param,historyFlg));
    }

    /**
     * 修改订单价格
     * @param param
     * @return
     */
    @PostMapping("/recalculate/price")
    private DefaultVO rePrice(@RequestBody @Valid ExpressRePriceParam param){
        return success(expressControlBiz.reCalculatePrice(param));
    }

    /**
     * 更换订单派遣司机
     * @param param
     * @return
     */
    @PostMapping("/redispatch/driver")
    private DefaultVO reDispatchDriver(@RequestBody @Valid DispatchDriverParam param){
        return success(expressControlBiz.reDispatchDriver(param));
    }

    /**
     * 站点驳车派单
     * @param param
     * @return
     */
    @PostMapping("/stp/dispatch/driver")
    private DefaultVO stpDispatchDriver(@RequestBody @Valid DispatchDriverParam param){
        return success(expressControlBiz.dispatchDriver(param));
    }

    /**
     * 虚拟接单
     * @param param
     * @return
     */
    @PostMapping("/virtual/accept")
    private DefaultVO virtualDispatchDriver(@RequestBody @Valid DispatchParam param){
        // todo 虚拟接单
        return success(expressControlBiz.virtualAcceptOrder(param));
    }

    /**
     * 取消站点驳车派单
     * @param param
     * @return
     */
    @PostMapping("/stp/undispatch/driver")
    private DefaultVO stpUnDispatchDriver(@RequestBody @Valid DispatchDriverParam param){
        return success(expressControlBiz.unDispatchDriver(param));
    }

    /**
     * 取消订单
     * @param param
     * @return
     */
    @PostMapping("/cancel/order")
    private DefaultVO cancelOrder(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(expressControlBiz.cancelOrder(idOrder,param.get("dispatcher")));
    }

    /**
     * 订单详情
     * @param param
     * @return
     */
    @PostMapping("/detail")
    private DefaultVO exOrderDetail(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(expressControlBiz.exOrderDetail(idOrder,false));
    }

    /**
     * 订单销毁
     * @param param
     * @return
     */
    @PostMapping("/destroy")
    private DefaultVO exOrderDestroy(@RequestBody @Valid ExOrderDestroyParam param){
        return success(expressControlBiz.exOrderDestroy(param));
    }

    /**
     * 修改备注
     * @param param
     * @return
     */
    @PostMapping("/re/memo")
    private DefaultVO exReMemo(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        String memo = param.get("memo");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(expressControlBiz.exReMemo(idOrder,memo));
    }

    /**
     *查询城市下所有快车订单
     */
    @PostMapping("/list/driver")
    private DefaultVO findByIdDriverAndStatus(@RequestBody Map<String,String> param){
        String idDriver=param.get("idDriver");
        return success(expressControlBiz.findByIdDriverAndStatus(idDriver, Constants.OrderStatus.ORDER_STATUS_ASSINGED));
    }

    /**
     * 订单支付信息
     * @param param
     * @return
     */
    @PostMapping("/billInfo")
    private DefaultVO orderBillInfo(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(expressControlBiz.getOrderBillInfo(idOrder,false));
    }

    @PostMapping("/list/settlement/view")
    private DefaultVO exOrderBillViewLst(@RequestBody @Valid ExSettlementViewListParam param){
        return success(expressControlBiz.exOrderBillViewLst(param));
    }

    /**
     * 订单修改
     * @param param
     * @return
     */
    @PostMapping("/info")
    private DefaultVO orderInfo(@RequestBody @Valid Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(expressControlBiz.orderInfo(idOrder));
    }

    /**
     * 历史订单检索查询
     * @param param
     * @return
     */
    @PostMapping("/search/history")
    private DefaultVO exOrderHistorySearch(@RequestBody @Valid ExListParam param){
        return success(expressControlBiz.exOrderSearch(param,true));
    }

    /**
     * 历史订单详情
     * @param param
     * @return
     */
    @PostMapping("/detail/history")
    private DefaultVO exOrderHistoryDetail(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(expressControlBiz.exOrderDetail(idOrder,true));
    }

    /**
     * 订单支付信息
     * @param param
     * @return
     */
    @PostMapping("/billInfo/history")
    private DefaultVO orderHistoryBillInfo(@RequestBody Map<String,String> param){
        String idOrder = param.get("idOrder");
        if (StrUtil.isEmpty(idOrder))
            throw new BusinessException(4001,"订单号[idOrder]不能为空");
        return success(expressControlBiz.getOrderBillInfo(idOrder,true));
    }
}
